package com.nxk.modules.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nxk.modules.content.dao.ConColunmDao;
import com.nxk.modules.content.entity.ConColunm;
import com.nxk.modules.content.service.ConColunmService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service
public class ConColunmServiceImpl extends ServiceImpl<ConColunmDao, ConColunm> implements ConColunmService {


   @Override
   @Transactional(readOnly = false)
    public boolean save(ConColunm conColunm) {
        if (StringUtils.isNotBlank(conColunm.getId())) {
            return super.updateById(conColunm);
        } else {
            conColunm.setColunmCode(UUID.randomUUID().toString());
            return super.save(conColunm);
        }
    }


    public List<ConColunm> listAll(ConColunm conColunm){
       QueryWrapper<ConColunm> queryWrapper=new QueryWrapper<>();
       if (StringUtils.isNotBlank(conColunm.getColunmName())) {
            queryWrapper.lambda().like(ConColunm::getColunmName, conColunm.getColunmName());
        }
       queryWrapper.orderByAsc("sort").in("status", 1);
       return super.list(queryWrapper);
    }


    @Override
    public IPage<ConColunm> page(ConColunm conColunm) {
        Page<ConColunm> page = new Page<ConColunm>(conColunm.getPageNo(), conColunm.getPageSize());
        QueryWrapper<ConColunm> queryWrapper = new QueryWrapper<ConColunm>();
        if (StringUtils.isNotBlank(conColunm.getColunmName())) {
            queryWrapper.lambda().like(ConColunm::getColunmName, conColunm.getColunmName());
        }
        queryWrapper.orderByAsc("sort").in("status", 1, 2);
        IPage<ConColunm> result = super.page(page, queryWrapper);
        return result;

    }

    @Override
    @Transactional(readOnly = false)
    public boolean deleteById(String id) {
        return super.removeById(id);
    }

}
